﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace _00365.Security
{
    public partial class UserManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadUsersData();
            }
        }

        private void LoadUsersData()
        {
            this.grdUsers.DataSource = Biz.Login.GetAll();
            this.grdUsers.DataBind();
        }

        protected void btnNewUser_Click(object sender, EventArgs e)
        {
            hidFlagAddEdit.Value = "Add";
            hidUserId.Value = string.Empty;            
            txtFullName.Text = string.Empty;
            txtUsername.Text = string.Empty;
            txtPassword.Text = string.Empty;
            txtConfirmPassword.Text = string.Empty;
            txtPassword.Enabled = true;
            txtConfirmPassword.Enabled = true;
            rdlActive.SelectedIndex = 0;
            lblNewUserDialogTitle.Text = "Incluir novo Usuário";
            popAddEditUser.Show();
            txtFullName.Focus();
        }

        protected void btnCloseNewUser_Click(object sender, EventArgs e)
        {
            popAddEditUser.Hide();
        }

        protected void btnSaveNewUser_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtUsername.Text.Trim()) || !String.IsNullOrEmpty(txtPassword.Text.Trim()))
            {
                if (hidFlagAddEdit.Value == "Edit")
                {
                    Data.Login newUser = new Data.Login();
                    newUser.Id = new Guid(hidUserId.Value);
                    newUser.FullName = txtFullName.Text.Trim();
                    newUser.Username = txtUsername.Text.Trim();                                        
                    newUser.Active = (rdlActive.SelectedValue == "True" ? true : false);
                    Biz.Login.Update(newUser);
                    hidUserId.Value = "";
                }
                else
                {
                    Data.Login newUser = new Data.Login();
                    newUser.Id = Guid.NewGuid();
                    newUser.FullName = txtFullName.Text.Trim();
                    newUser.Username = txtUsername.Text.Trim();
                    newUser.Password = txtPassword.Text.Trim();
                    newUser.Active = (rdlActive.SelectedValue == "True" ? true : false);
                    Biz.Login.Add(newUser);
                }
                LoadUsersData();
            }
        }

        protected void grdUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Data.Login user = null;
            if (e.CommandName != string.Empty && e.CommandArgument.ToString().Length == 36)
                user = Biz.Login.Get(new Guid(e.CommandArgument.ToString()));

            switch (e.CommandName)
            {                
                case "EditUser":
                    if (user != null)
                    {
                        lblNewUserDialogTitle.Text = "Atualizar Informações do Usuário";
                        txtFullName.Text = user.FullName;
                        txtUsername.Text = user.Username;
                        //txtPassword.Text = user.Password;
                        //txtConfirmPassword.Text = user.Password;
                        lblPassword.Visible = false;
                        lblConfirmPassword.Visible = false;
                        txtPassword.Visible = false;
                        txtConfirmPassword.Visible = false;
                        rdlActive.SelectedValue = user.Active.ToString();
                        hidUserId.Value = user.Id.ToString();
                        hidFlagAddEdit.Value = "Edit";
                        this.popAddEditUser.Show();
                    }
                    break;
                case "DeleteUser":
                    hidUserId.Value = user.Id.ToString();
                    lblDeleteMsg.Text = "Deseja realmente excluir o usuário \"" + user.Username + "\"?";
                    popDeleteMsg.Show();
                    break;
                case "ChangePassword":
                    hidUserId.Value = user.Id.ToString();                    
                    popChangePassword.Show();
                    break;
                default:
                    break;
            }
        }

        protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        { grdUsers.PageIndex = e.NewPageIndex; }

        protected void grdUsers_PageIndexChanged(object sender, EventArgs e)
        { LoadUsersData(); }

        protected void btnDeleteMsgYes_Click(object sender, EventArgs e)
        {
            Data.Login user = null;

            if (hidUserId.Value != string.Empty && hidUserId.Value.ToString().Length == 36)
                user = Biz.Login.Get(new Guid(hidUserId.Value.ToString()));
            if (user != null)
            {
                Biz.Login.Remove(user.Id);
                LoadUsersData();
            }
            popDeleteMsg.Hide();
        }

        protected void btnDeleteMsgNo_Click(object sender, EventArgs e)
        {
            popDeleteMsg.Hide();
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            Data.Login user = Biz.Login.Get(new Guid(hidUserId.Value));
            Biz.Login.ChangePassword(user, txtChangePassword.Text);
            hidUserId.Value = "";
        }
    }
}